package org.delphy.indy2.containers.carbonFireworksBoxClasses
{
	import caurina.transitions.Tweener;
	
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.events.TimerEvent;
	import flash.geom.Point;
	import flash.utils.Timer;
	
	import mx.containers.Canvas;
	import mx.controls.Image;
	import mx.effects.Fade;
	import mx.effects.Parallel;
	import mx.effects.WipeUp;
	import mx.events.EffectEvent;
	import mx.managers.PopUpManager;
	import mx.styles.StyleManager;
	
	import org.delphy.indy2.core.ICarbonComponent;

	//--------------------------------------
	//  Styles
	//--------------------------------------
	
	/**
	 * The flame bitmap
	 * 
	 * @default org/delphy/indy2/assets/images/carbonFireworks/flame.png
	 */
	[Style(name="flameImage", type="Class", inherit="no")]
	
	/**
	 * Flame width
	 * 
	 * @default 80
	 */
	[Style(name="flameWidth", type="Number", inherit="no")]
	
	/**
	 * Flame height
	 * 
	 * @default 96
	 */
	[Style(name="flameHeight", type="Number", inherit="no")]
	
	public class CarbonFireworksLauncher extends Canvas implements ICarbonComponent
	{

		//--------------------------------------
		//  Bitmaps
		//--------------------------------------
		
		/**
		 * @protected
		 * 
		 * @default org/delphy/indy2/assets/images/carbonFireworks/flame.png
		 */
		protected var flame:Image;
		
		//--------------------------------------
		//  Animation
		//--------------------------------------
		
		/**
		 * @public
		 * 
		 * @default 20
		 */
		public var shakeBoxMaxSpringHeight:Number=20;
		
		/**
		 * @protected
		 */
		protected var shakeBoxNextSpringHeight:Number;
		
		/**
		 * @public
		 * 
		 * @default 2
		 */
		public var shakeTransitionTime:Number=2;
		
		/**
		 * @protected
		 */
		protected var wipe:WipeUp;
		
		/**
		 * @protected
		 */
		protected var fadeIn:Fade;

		/**
		 * @protected
		 */
		protected var fadeOut:Fade;
		
		/**
		 * @protected
		 */		
		protected var flameEffect:Parallel;
		
		/**
		 * @public
		 * 
		 * @default 0.2
		 */		
		public var flameTransitionTime:Number=0.2;
		
		//--------------------------------------
		//  Positions
		//--------------------------------------
		
		/**
		 * @protected
		 */
		protected var origY:Number;
		
		/**
		 * @protected
		 */
		protected var origFlameY:Number;
		
		//--------------------------------------
		//  Methods
		//--------------------------------------
		
		/**
		 * Constructor
		 */
		public function CarbonFireworksLauncher()
		{
			super();
			
			addEventListener(MouseEvent.CLICK, launch);
		}
		
		override protected function measure() : void{
			super.measure();
			
			measuredWidth=47;
			measuredHeight=26;
		}
		
		/**
		 * @protected
		 */
		override protected function commitProperties() : void{
			super.commitProperties();
			
			// Fallback to default skin
			if(!styleName)
				styleName="carbonFireworksLauncher";
			
			if(!getStyle("backgroundImage"))
				setStyle("backgroundImage", StyleManager.getStyleDeclaration(".carbonFireworksLauncher").getStyle("backgroundImage"));
			
			if(!getStyle("backgroundSize"))
				setStyle("backgroundSize", StyleManager.getStyleDeclaration(".carbonFireworksLauncher").getStyle("backgroundSize"));			
		}

		/**
		 * @public
		 */
		public function launch(evt:MouseEvent=null):void{
			flame=new Image();
			
			if(getStyle("flameWidth"))
				flame.width=getStyle("flameWidth");
			else
				flame.width=StyleManager.getStyleDeclaration(".carbonFireworksLauncher").getStyle("flameWidth");
			
			if(getStyle("flameHeight"))
				flame.height=getStyle("flameHeight");
			else
				flame.height=StyleManager.getStyleDeclaration(".carbonFireworksLauncher").getStyle("flameHeight");
			
			if(getStyle("flameImage"))
				flame.source=getStyle("flameImage");
			else
				flame.source=StyleManager.getStyleDeclaration(".carbonFireworksLauncher").getStyle("flameImage");
			
			var pt:Point=new Point(x,y);
			pt=parent.localToGlobal(pt);
			
			flame.x=pt.x+width/2-flame.width/2;
			flame.y=pt.y+height-flame.height;
			
			PopUpManager.addPopUp(flame,this,false);
			
			shake();
		}
		
		/**
		 * @protected
		 */
		protected function shake():void{
			var timer:Timer=new Timer(shakeTransitionTime*1000,1);
			timer.addEventListener(TimerEvent.TIMER,onShakeComplete);
			timer.start();
			
			shakeBoxNextSpringHeight=shakeBoxMaxSpringHeight;
			
			origY=y;
			origFlameY=flame.y;
			
			doShakeUpAnimation();
			
			wipe=new WipeUp();
			
			fadeIn=new Fade();
			fadeIn.alphaFrom=0;
			fadeIn.alphaTo=1;
			
			flameEffect=new Parallel(flame);
			flameEffect.addChild(wipe);
			flameEffect.addChild(fadeIn);
			flameEffect.duration=flameTransitionTime*1000;
			
			flameEffect.play();		
		}
		
		/**
		 * @protected
		 */
		protected function doShakeUpAnimation():void{
			var nextHeight:Number=shakeBoxNextSpringHeight/2;
			Tweener.addTween(this,{time:shakeTransitionTime/6,y:origY-shakeBoxNextSpringHeight,transition:"linear",onComplete:doShakeDownAnimation});
			Tweener.addTween(flame,{time:shakeTransitionTime/6,y:origFlameY-shakeBoxNextSpringHeight,transition:"linear"});
			
			shakeBoxNextSpringHeight=nextHeight;
		}
		
		/**
		 * @protected
		 */
		protected function doShakeDownAnimation():void{
			Tweener.addTween(this,{time:shakeTransitionTime/6,y:origY,transition:"linear",onComplete:doShakeUpAnimation});
			Tweener.addTween(flame,{time:shakeTransitionTime/6,y:origFlameY,transition:"linear"});
		}
		
		/**
		 * @protected
		 */
		protected function onShakeComplete(evt:Event=null):void{
			Tweener.removeTweens(this);
			Tweener.removeTweens(flame);
			
			fadeOut=new Fade(flame);
			fadeOut.alphaFrom=1;
			fadeOut.alphaTo=0;
			fadeOut.duration=flameTransitionTime*1000;
			fadeOut.addEventListener(EffectEvent.EFFECT_END,onFlameFinished);
			fadeOut.play();	
		}
		
		/**
		 * @protected
		 */
		protected function onFlameFinished(evt:Event=null):void{
			PopUpManager.removePopUp(flame);
		}
		
		public function get defaultStyleName():String{
			return "carbonFireworksLauncher";
		}
	}
}